define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'adminlte', 'template'], function ($, undefined, Backend, Table, Form, Adminlte, Template) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'teacher/index',
                    add_url: 'teacher/add',
                    edit_url: 'teacher/edit',
                    del_url: 'teacher/del',
                    multi_url: 'teacher/multi',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'teacher.id',
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {field: 'name', title: __('姓名'), operate: false},
                        {field: 'gender_text', title: __('性别'), operate: false},
                        {field: 'mobile', title: __('手机号'), operate: false},
                        {
                            field: 'created_at',
                            title: __('Create time'),
                            sortable: true,
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange'
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                commonSearch: false,
                searchFormVisible: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $('.group_type').on('change', function () {
                    if ($(this).val() === '2') {
                        var timestamp = Date.parse(new Date()) + 7 * 86400000;
                        var date = Controller.api.getMyDate(timestamp);
                        $('input[name=group_end_time]').val(date);
                        $('#group_end_time').show();

                        //$('input[name=group_end_time]').data('rule', 'required');
                    } else {
                        $('#group_end_time').hide();
                        //$('input[name=group_end_time]').data('rule', '');
                    }
                });

                Form.api.bindevent($("form[role=form]"));
                Form.events.datetimepicker($("form[role=form]"));
            },
            getMyDate: function (str) {
                var oDate = new Date(str),
                    oYear = oDate.getFullYear(),
                    oMonth = oDate.getMonth() + 1,
                    oDay = oDate.getDate(),
                    oHour = oDate.getHours(),
                    oMin = oDate.getMinutes(),
                    oSen = oDate.getSeconds(),
                    oTime = oYear + '-' + Controller.api.getzf(oMonth) + '-' + Controller.api.getzf(oDay) + ' ' + Controller.api.getzf(oHour) + ':' +
                        Controller.api.getzf(oMin) + ':' + Controller.api.getzf(oSen);//最后拼接时间
                return oTime;
            },
            getzf: function (num) {
                if (parseInt(num) < 10) {
                    num = '0' + num;
                }
                return num;
            },
        }
    };
    return Controller;
});